package demo6;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;

public class CardDemo {
    public static final String suits[]={"♥","♠","♣","♦"};
    //买牌
    public List<Card> buyCard(){
        List<Card> cardList=new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for(int j=0;j<4;j++){
                int rank=i;
                String  suit=suits[j];
                Card card=new Card(suit,rank);
                cardList.add(card);
            }
        }
        return cardList;
    }
    //洗牌
    public void shuffle(List<Card> cardList){
        Random random=new Random();
        for(int i=cardList.size()-1;i>0;i--){
            int index=random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    private void swap(List<Card> cardList,int i,int j){
        Card tmp=cardList.get(i);
        cardList.set(i,cardList.get(j));
        //cardList.set(j,cardList.get(i));
        cardList.set(j,tmp);

    }
    //3个人
    public List<List<Card>> play(List<Card> cardList) {
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //hand.get(i).get(j)=cardList.remove(0);
               //下面两行代码很重要
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }

}
